﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace HaiFang
{
    /// <summary>
    /// Summary description for ModuleInfoHandler
    /// </summary>
    public class ModuleInfoHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["action"]))
            {
                string action = context.Request.QueryString["action"];
                switch (action)
                {
                    case "GetModuleInfo":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseModuleInfo(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作.");
                        break;
                }
            }
            else if (context.Request.Form.Count > 0)
            {
                string action = context.Request.Form["action"];
                switch (action)
                {
                    case "ImportModule":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseImportModule(context, Params);
                            }
                        }
                        break;
                    case "AddModuleInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseAddModuleInfo(context, Params);
                            }
                        }
                        break;
                    case "DelModuleInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseDelModuleInfo(context, Params);
                            }
                        }
                        break;
                    case "SaveModuleInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseSaveModuleInfo(context, Params);
                            }
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作");
                        break;
                }
            }
        }

        private void ResponseModuleInfo(HttpContext context, string Params)
        {
            try
            {
                IList<ModuleInfo> list = ProxyModuleInfo.QueryModuleInfo(WebHelper.GetCurrentUserID(), 
                    WebHelper.GetCurrentUserProjectID(), 0);
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseImportModule(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                if (!String.IsNullOrEmpty(Params))
                {
                    ProxyModuleInfo.ImportModule(WebHelper.GetCurrentUserID(), Convert.ToInt32(Params), 
                        WebHelper.GetCurrentUserProjectID(),out ErrorMSG);
                    WebHelper.WriteJsonResult(context, ErrorMSG);
                    return;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseAddModuleInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleInfo>(Params);
                    if (r != null)
                    {
                        ProxyModuleInfo.InsertModuleInfo(WebHelper.GetCurrentUserID(), 1, 0, WebHelper.GetCurrentUserProjectID(),
                            r.ModuleName, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseDelModuleInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleInfo>(Params);
                    if (r != null)
                    {
                        ProxyModuleInfo.InsertModuleInfo(WebHelper.GetCurrentUserID(), 3, r.AutoID, WebHelper.GetCurrentUserProjectID(),
                            r.ModuleName, r.Description, out ErrorMSG); 
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseSaveModuleInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleInfo>(Params);
                    if (r != null)
                    {
                        ProxyModuleInfo.InsertModuleInfo(WebHelper.GetCurrentUserID(), 2, r.AutoID, WebHelper.GetCurrentUserProjectID(),
                            r.ModuleName, r.Description, out ErrorMSG); 
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}